package sparrowGui.components.item
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import sparrowGui.event.ItemEvent;
	
	/** 项数据更新. **/
	[Event(name="item_update", 	type="sparrowGui.event.ItemEvent")]
	/** 项选中状态改变. **/
	[Event(name="item_select_change", 	type="sparrowGui.event.ItemEvent")]
	/** 项可用状态改变. **/
	[Event(name="item_ebable_change", 	type="sparrowGui.event.ItemEvent")]
	
	/**
	 * 列表项，有六状态，有经过移动事件，不过没有选中事件
	 * 
	 * 例子如下
	 * 
	 * var itm:SItem = new SListItem();
		itm.update("按钮文字");
		addChild(itm);
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SListItem extends SButton
	{
		static public const SELECTSTATE_NAME:String = "selectState";
		static public const ENABLEDSTATE_NAME:String = "enabledState";
		
		protected var _selectState:DisplayObject;
		protected var _enabledState:DisplayObject;
		
		/**
		 * 选中状态
		 */
		private var _selected:Boolean;
		private var _data:Object;
		
		public function SListItem(uiVars:Object=null)
		{
			super(uiVars);
		}
		
		override public function update(o:Object):void
		{
			_data = o;
			super.update(o);
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_UPDATE));
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
			super.setUI(uiVars);
			
			if(skin is DisplayObjectContainer)
			{
				var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
				skinDC.mouseChildren = false;
				
				_selectState = skinDC.getChildByName(SELECTSTATE_NAME);
				_enabledState = skinDC.getChildByName(ENABLEDSTATE_NAME);
			}
			
			_selected = false;
			super.enabled = true;
		}
		
/*		override protected function addSkinListen():void
		{
			super.addSkinListen();
			skin.addEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		override protected function removeSkinListen():void
		{
			super.removeSkinListen();
			skin.removeEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		protected function onMouseClick(e:MouseEvent):void
		{
			if(!enabled)
				return;
			
			selected = !selected;
		}*/
		
		override protected function onEvtOut(e:Event):void
		{
			// 选中的时候把其它的事件过滤
			if(selected)
				return;
			
			super.onEvtOut(e);
		}
		
		override protected function onMouseDown(e:MouseEvent):void
		{
			if(selected) return;
			super.onMouseDown(e);
		}
		
		override protected function onRollOut(e:MouseEvent):void
		{
			if(selected) return;
			super.onRollOut(e);
		}
		
		override protected function onRollOver(e:MouseEvent):void
		{
			if(selected) return;
			super.onRollOver(e);
		}
		
		override public function setState(stateName:String,value:Object=null):void
		{
			switch(stateName)
			{
				case SELECTSTATE_NAME:
				{
					selected = value;
					return;
				}
				case ENABLEDSTATE_NAME:
				{
					enabled = value;
					return;
				}
			}
			// 处于选中状态或者不可用状态时不能改变其它状态
			if(selected || !enabled)
				return;
			
			super.setState(stateName);
		}
		
		override protected function get states():Array
		{
			return [upState,overState,downState,selectState,enabledState];
		}
		
/*		override protected function hiddenState():void
		{
			var itemStates:Array = [upState,overState,downState,hitTestState,selectState,enabledState];
			for each(var dp:DisplayObject in itemStates)
			if(dp) dp.visible = false;
		}*/
		
		///////////////////////////////////
		// get/set
		//////////////////////////////////
		
		
		override public function getDefaultUIName():String
		{
			return "richItem";
		}
		
		/**
		 * 选中状态
		 */
		override public function set selected(value:Boolean):void
		{
			// 状态跟原状态不同时才重设
			if(_selected != value )
			{
				if(value){
					super.removeSkinListen();
					changeState(SELECTSTATE_NAME);
				}else{
//					removeSkinListen();
					super.addSkinListen();
					changeState(UPSTATE_NAME);
				}
				_selected = value;
			}
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_SELECT_CHANGE));
		}
		
		override public function get selected():Boolean
		{
			return _selected;
		}
		
		/**
		 * 能否使用
		 */
		override public function set enabled(value:Boolean):void
		{
			// 状态跟原状态不同时才重设
			if(super.enabled!=value)
			{
				super.enabled = value;
				if(super.enabled){
//					selected = selected;
//					removeSkinListen();
					if(selected){
						changeState(SELECTSTATE_NAME);
					}else{
						changeState(UPSTATE_NAME);
					}
					addSkinListen();
				}else{
					changeState(ENABLEDSTATE_NAME);
					removeSkinListen();
				}
				if(skin is InteractiveObject)
					(skin as InteractiveObject).mouseEnabled = value;
			}
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_EBABLE_CHANGE));
		}
		
		override public function get itemData():Object 
		{
			return _data;
		}
		
		override public function dispose():void
		{
			selected = false;
			enabled = true;
			_data = null;
			super.dispose();
		}

		public function get selectState():DisplayObject
		{
			return _selectState;
		}

		public function get enabledState():DisplayObject
		{
			return _enabledState;
		}
	}
}